using Ant.DbServiceInterface;
using Ant.Dto.PayDto;
using Ant.EntityFrameworkCore.Model;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using System.Linq;
using System;
using System.Threading.Tasks;
using System.Globalization;

namespace Ant.DbService
{
    public class PayDataService : IPayDataService
    {

        private AntDB2Context _dbContext;
        public PayDataService(AntDB2Context dbContext)
        {
            _dbContext = dbContext;
        }

        public bool CreateOrder(int storeID, int salesman, ICollection<CommodityOrder> commodityOrder)
        {
            var tran = _dbContext.Database.BeginTransaction();
            try
            {

                decimal sumMoney = 0, discount = 0;
                foreach (var item in commodityOrder)
                {
                    sumMoney += item.CopeWith;
                    discount += item.CopeWith;
                }
                var info = new SalesOrder()
                {
                    Sum = sumMoney,
                    Discount = discount,
                    StroeId = storeID,
                    Salesman = salesman
                };
                _dbContext.SalesOrders.Add(info);
                _dbContext.SaveChanges();
                foreach (var item in commodityOrder)
                {
                    item.SalesOrderId = info.SalesId;
                    _dbContext.Add(item);
                }
                _dbContext.SaveChanges();
                tran.Commit();
                return true;
            }
            catch (Exception)
            {
                tran.Rollback();
                return false;
            }
        }

        public async Task<ICollection<SalesOrder>> GetOrder(int storeID)
        {
            var query = _dbContext.SalesOrders
                                .Include(s => s.CommodityOrders)
                                .Include(s => s.Stroe)
                                .Include(s => s.SalesmanNavigation)
                                .Where(s => s.StroeId == storeID)
                                .ToListAsync();
            return await query;
        }

        public async Task<ICollection<Series>> GetStatisticalBy1M(int storeID, DateTime start)
        {


            List<Series> series = new List<Series>();
            var query = _dbContext.SalesOrders.Where(s => s.StroeId == storeID);
            var time = new DateTime(start.Year, start.Month, 1,0,0,0);
            if (start.Ticks == 0)
            {
                time = new DateTime(DateTime.Now.Year,DateTime.Now.Month,1);
            }
            // var data = new GetStatisticalDto();


            series.Add(
                    new Series()
                    {
                        Name = "交易笔数",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );

            series.Add(
                    new Series()
                    {
                        Name = "总交易额",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );
            series.Add(
                    new Series()
                    {
                        Name = "平均客单价",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );

            for (int i = 0; i < 31; i++)
            {
                if (i != 0)
                {
                    if (time.AddDays(i).Day == 1)
                    {
                        break;
                    }
                }

                start = new DateTime(time.Year, time.Month, time.AddDays(i).Day, 0, 0, 0);
                var end = new DateTime(time.Year, time.Month, time.AddDays(i + 1).Day,0, 0, 0);

                var temp = await query.Where(s => s.Time > start && s.Time < end).ToListAsync();
                if (temp.Count != 0)
                {
                    //有数据
                    series[0].Data.Add(temp.Count);
                    series[1].Data.Add(temp.Sum(s => s.Discount));
                    series[2].Data.Add(temp.Average(s => s.Discount));
                }
                else
                {
                    //没数据
                    series[0].Data.Add(0);
                    series[1].Data.Add(0);
                    series[2].Data.Add(0);
                }
            }
            return series;
        }

        public async Task<ICollection<Series>> GetStatisticalBy24(int storeID, DateTime start)
        {
            List<Series> series = new List<Series>();
            var query = _dbContext.SalesOrders.Where(s => s.StroeId == storeID);
            var time = start;
            if (start.Ticks == 0)
            {
                time = DateTime.Now;
            }


            series.Add(
                    new Series()
                    {
                        Name = "交易笔数",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );

            series.Add(
                    new Series()
                    {
                        Name = "总交易额",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );
            series.Add(
                    new Series()
                    {
                        Name = "平均客单价",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );



            for (int i = 0; i < 24; i++)
            {
                start = new DateTime(time.Year, time.Month, time.Day, i, 0, 0);
                var end = new DateTime(time.Year, time.Month, time.Day, i + 1 == 24 ? 0 : i + 1, 0, 0);

                var temp = await query.Where(s => s.Time > start && s.Time < end).ToListAsync();
                if (temp.Count != 0)
                {
                    //有数据
                    series[0].Data.Add(temp.Count);
                    series[1].Data.Add(temp.Sum(s => s.Discount));
                    series[2].Data.Add(temp.Average(s => s.Discount));
                }
                else
                {
                    //没数据
                    series[0].Data.Add(0);
                    series[1].Data.Add(0);
                    series[2].Data.Add(0);
                }
            }
            return series;
        }

        public async Task<ICollection<Series>> GetStatisticalBy7D(int storeID, DateTime start)
        {
            List<Series> series = new List<Series>();
            var query = _dbContext.SalesOrders.Where(s => s.StroeId == storeID);
            var time = start;
            if (start.Ticks == 0)
            {
                time = DateTime.Now;
            }


            series.Add(
                    new Series()
                    {
                        Name = "交易笔数",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );

            series.Add(
                    new Series()
                    {
                        Name = "总交易额",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );
            series.Add(
                    new Series()
                    {
                        Name = "平均客单价",
                        Type = "line",
                        Stack = "总量",
                        Data = new List<decimal>()
                    }
                );

                time = time.AddDays(-6);
            for (int i = 0; i < 7; i++)
            {

                start = new DateTime(time.Year, time.Month, time.Day);
                time = time.AddDays(1);
                var end = new DateTime(time.Year, time.Month, time.Day);

                var temp = await query.Where(s => s.Time > start && s.Time < end).ToListAsync();
                if (temp.Count != 0)
                {
                    //有数据
                    series[0].Data.Add(temp.Count);
                    series[1].Data.Add(temp.Sum(s => s.Discount));
                    series[2].Data.Add(temp.Average(s => s.Discount));
                }
                else
                {
                    //没数据
                    series[0].Data.Add(0);
                    series[1].Data.Add(0);
                    series[2].Data.Add(0);
                }
            }
            return series;
        }
    }
}